package org.jeecg.modules.app.order.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeCreateModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.response.AlipayTradeCreateResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.miniapp.AliConfig;
import org.jeecg.modules.app.order.service.AliPayService;
import org.jeecg.modules.app.util.AliPayConfig;
import org.jeecg.modules.power.user.entity.YlUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.app.order.service.impl
 * @Author: mi
 * @CreateTime: 2024-07-13 14:49
 * @Description:
 */
@Service
public class AliPayServiceImpl implements AliPayService {

    @Autowired
    private AliConfig aliConfig;//支付宝

    /**
     * 支付宝 小程序支付
     * @param payNumber 支付单号
     * @param totalAmount 支付金额
     * @param describe 描述信息
     * @param user 用户信息
     * @return
     */
    @Override
    public Result<?> returnAliPayData(String payNumber, String totalAmount, String describe, YlUser user) throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(AliPayConfig.gatewayUrl);
        certAlipayRequest.setAppId(aliConfig.getAppId());//支付宝用户
        certAlipayRequest.setPrivateKey(AliPayConfig.merchant_private_key);//应用 私钥
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("UTF-8");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setCertPath(aliConfig.getCertPath());
        certAlipayRequest.setAlipayPublicCertPath(aliConfig.getPublicCertPath());
        certAlipayRequest.setRootCertPath(aliConfig.getRootCertPath());
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        // 构造请求参数以调用接口
        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
        AlipayTradeCreateModel model = new AlipayTradeCreateModel();
        // 设置商户订单号
        model.setOutTradeNo(payNumber);
        // 设置产品码
        model.setProductCode(AliPayConfig.JS_API);
        // 设置小程序支付中
        model.setOpAppId(user.getAliUserId());
        // 设置订单总金额
        model.setTotalAmount(totalAmount);
        model.setBody(describe);
        model.setSubject(describe);
        model.setTimeoutExpress("30m");
        request.setBizModel(model);
      // 回调地址
        request.setNotifyUrl(aliConfig.getNotifyUrl());// 回调地址

        //这里和普通的接口调用不同，使用的是sdkExecute
        AlipayTradeCreateResponse response = alipayClient.sdkExecute(request);
        Map<String, String> map = new HashMap<String, String>();
        if (response.isSuccess()) {
            System.out.println("调用成功+==="+response.getBody());
            map.put("data", response.getBody());
            return Result.OK(map);
        } else {
            System.out.println("调用失败+==="+response.getBody());
        }
        return  Result.error_599("支付宝调用失败");
    }
}
